package com.css.common.core.enums;

import com.css.common.core.utils.BaseUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.css.common.core.enums.PayKpEnum.*;


/**
 * @description: 农产品订单状态
 * @Title: NcpOrderStatusEnum
 * @Author xlw
 * @Package com.invoice.tcc.enums
 * @Date 2024/10/29 10:56
 */
@AllArgsConstructor
@Getter
public enum FxOrderStatusEnum {
    WAIT_AUDIT("9", "待审核"),

    AUDIT_FAIL("10", "审核失败"),

    CONFIRM("11", "订单待确认"),//待付款确认

    WAIT_PAY("21", "待支付"),

    PAYING("22", "支付中"),

    PAY_FAIL("23", "支付失败"),

    KPING("31", "开票中"),

    KP_FAIL("32", "开票失败"),

    KP_CONFIRM("33", "开票待确认"),

    LP_SUCCESS("41", "蓝票成功"),

    HP_SUCCESS("51", "红票成功"),

    CANCELED("61", "已取消"),

    RETURN("62", "红票开具中"),

    TAX_DC("71", "税费信息待查询"),

    TAX_DC_FAIL("72", "税费信息查询失败"),

    TAX_FREE("81", "免税"),

    TAX_DJS("82", "待缴税"),

    TAX_JS_FAIL("83", "缴税失败"),

    TAX_JS_SUCCESS("84", "缴税成功"),

    ;
    private String code;

    private String desc;

    /**
     * 界面展示的订单状态
     */
    private static List<Dmmc> fxOrderStatusGroup;

    static {
        fxOrderStatusGroup = new ArrayList<Dmmc>();
        //待确认（待付款确认），付款中 付款失败 开票中 已完成（蓝字发票） 已完成(红字发票) 已取消
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{CONFIRM.getCode()}), "待确认"));
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{WAIT_PAY.getCode(), PAYING.getCode()}), "付款中"));
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{PAY_FAIL.getCode()}), "付款失败"));
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{KPING.getCode(), KP_CONFIRM.getCode(), RETURN.getCode()}), "开票中"));
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{KP_FAIL.getCode()}), "开票失败"));
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{LP_SUCCESS.getCode()}), "已完成(蓝字发票)"));
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{HP_SUCCESS.getCode()}), "已完成(红字发票)"));
        fxOrderStatusGroup.add(new Dmmc(Arrays.asList(new String[]{CANCELED.getCode()}), "已取消"));
    }

    /**
     * @Param ddzt 订单状态
     */
    public boolean equals(String ddzt) {
        return this.getCode().equals(ddzt);
    }


    /**
     * 根据支付开票确认方式代码获取订单状态代码
     */
    public static String getDmByZfkpqrfsDm(String zfkpqrfsDm) {
        final FxOrderStatusEnum e = getByZfkpqrfsDm(zfkpqrfsDm);
        return BaseUtil.isNull(e) ? null : e.getCode();
    }

    /**
     * 根据支付开票确认方式代码获取订单状态枚举
     */
    public static FxOrderStatusEnum getByZfkpqrfsDm(String zfkpqrfsDm) {
        return getByZfkpqrfsEnum(PayKpEnum.getInstance(zfkpqrfsDm));
    }

    /**
     * 根据支付开票确认方式枚举获取订单状态枚举
     */
    public static FxOrderStatusEnum getByZfkpqrfsEnum(PayKpEnum e) {
        if (BaseUtil.isNull(e)) return null;
        if (AUTO_PAY_AUTO_KP.equals(e) || AUTO_PAY_CONFIRM_KP.equals(e)) {
            return WAIT_PAY;
        } else if (CONFIRM_PAY_AUTO_KP.equals(e) || PayKpEnum.CONFIRM_PAY_CONFIRM_KP.equals(e)) {
            return CONFIRM;
        } else {
            return null;
        }
    }

    /**
     * 根据支付开票确认方式代码获取开票状态
     */
    public static String getKpztByZfkpqrfsDm(String zfkpqrfsDm) {
        if (BaseUtil.isNull(zfkpqrfsDm)) return null;
        if (AUTO_PAY_AUTO_KP.getDm().equals(zfkpqrfsDm) || CONFIRM_PAY_AUTO_KP.getDm().equals(zfkpqrfsDm)) {
            return KPING.getCode();
        } else if (AUTO_PAY_CONFIRM_KP.getDm().equals(zfkpqrfsDm) || CONFIRM_PAY_CONFIRM_KP.getDm().equals(zfkpqrfsDm)) {
            return KP_CONFIRM.getCode();
        } else {
            return null;
        }
    }

    /**
     * 获取反向订单分组状态
     */
    public static List<Dmmc> getFxOrderStatusGroup() {
        return fxOrderStatusGroup;
    }

    /**
     * 获取反向订单展示状态名称
     */
    public static String getMcForShow(String dm) {
        final Dmmc dmmc = fxOrderStatusGroup.stream().filter(e -> e.dm.contains(dm)).findFirst().orElse(null);
        return BaseUtil.isNull(dmmc) ? null : dmmc.getMc();
    }

    public static FxOrderStatusEnum getInstance(String code) {
        for (FxOrderStatusEnum statusEnum : values()) {
            if (statusEnum.getCode().equals(code)) {
                return statusEnum;
            }
        }
        return null;
    }

    /**
     * 转换
     *
     * @param payKpEnum   支付 kp 枚举
     * @param isSystemPay 是系统付费
     * @return {@link FxOrderStatusEnum }
     */
    public static FxOrderStatusEnum convert(PayKpEnum payKpEnum, boolean isSystemPay) {
        if (isSystemPay) {
            //系统支付
            switch (payKpEnum) {
                case AUTO_PAY_AUTO_KP:
                case AUTO_PAY_CONFIRM_KP:
                    return WAIT_PAY;
                case CONFIRM_PAY_AUTO_KP:
                case CONFIRM_PAY_CONFIRM_KP:
                    return CONFIRM;
                default:
                    return WAIT_PAY;
            }
        }
        //非系统支付
        switch (payKpEnum) {
            case AUTO_PAY_AUTO_KP:
                return KPING;
            case AUTO_PAY_CONFIRM_KP:
                return KP_CONFIRM;
            default:
                return KPING;
        }
    }


    /**
     * 代码、名称
     */
    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    public static class Dmmc {
        @ApiModelProperty(value = "代码码值")
        @JsonProperty("dm")
        private List<String> dm;

        @ApiModelProperty(value = "代码名称")
        @JsonProperty("mc")
        private String mc;


    }

}
